package es.ats.codington.festival.junit;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


import es.ats.codington.festival.daos.EventDAOImpl;
import es.ats.codington.festival.daos.EventSignUpDAOImpl;
import es.ats.codington.festival.daos.PlaceDAOImpl;
import es.ats.codington.festival.daos.VisitorDAOImpl;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.People;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.exceptions.DAOException;
import es.ats.codington.festival.utils.FERSDataConnection;
import es.ats.codington.festival.domain.Event;
/**
 * 
 * @author JAVA101_01
 *
 */
public class TEventSignUpDAO extends TestCase{
	
	Connection conn;
	PlaceDAOImpl placeDAO;
	EventSignUpDAOImpl eventSignUpDAO;	
	VisitorDAOImpl visitorDAO;
	EventSignUp eventSignUp1;
	EventDAOImpl eventDAO;
    Event event1, event2, event;
    People p1,people;
    Visitor v1,visitor;
    Place place1, place;
    
    /**
	 * SetUp of the test
	 * @throws Exception
	 */
	@Before
	public void setUp() throws Exception {
		super.setUp();
		conn = FERSDataConnection.getConnection();
		eventSignUpDAO = new EventSignUpDAOImpl(conn); 
		visitorDAO = new VisitorDAOImpl(conn);
		eventDAO = new EventDAOImpl(conn);
		placeDAO = new PlaceDAOImpl(conn);
	}

	/**
	 * TearDown of the test
	 * @throws Exception
	 */
	@After
	public void tearDown() throws Exception {
         super.tearDown();
       
		
	}
	
	public Place createPlace() {
		place1 = new Place();
		
		place1.setNamePlace("Codington Arena");
		place1.setCapacityPlace(100000);
		place1.setPlaceDescription("Footbal Stadium");
		place1.setZonePlace("NORTH");
		place1.setImagePlace("");
		
		
		return place1;
	}

	public People createPeople(){
		
		p1 = new People();
		
		p1.setFirstName("Jose");
		p1.setLastName("Martinez Nieto");
		p1.setDni("32.888.932-Y");
		p1.setEmail("jose.martinez.nieto@gmail.com");
		p1.setPhoneNumber("689658945");
		p1.setAddress("C/Gran Via");
		
		return p1;
		
	}
	
	public Visitor createVisitor() {
		
		v1 = new Visitor();
		
		v1.setUserName("Jose1");
		v1.setPassword("josemola33");
		v1.setAdmin(false);
		v1.setVisitorId(people.getIdPeople());
		return v1;
	}
	
	public Event createEvent() {
		
		event2 = new Event();
		
		event2.setName("Madrid-Barcelona");
		event2.setDescription("Football match");
		event2.setPlace("Bernabeu");
		event2.setStartEvent("20:45");
		event2.setEndEvent("22:30");
		event2.setDayEvent("06/Marzo/2015");
		event2.setEventType("Sports");
		event2.setSeatsAvailable(90000);
		
		
		
		return event2;
		
	}

	/**
	 * Test for the RegisterVisitorToEvent method
	 */
	@Test
	public void testRegisterVisitorToEvent() {
		boolean affectedRows;
		people = new People();
		people = this.createPeople();
		visitorDAO.insertData(people);
		
		visitor = new Visitor();
		visitor = this.createVisitor();
		visitorDAO.insertData(visitor);
		
		place = new Place();
		place = this.createPlace();
		
		placeDAO.insertPlace(place, "Stadium");
		
		event = new Event();
		event = this.createEvent();
		event.setIdPlace(place.getId());
		
		eventDAO.insertEvent(event);
		
        eventSignUp1 = new EventSignUp();
		
		eventSignUp1.setIdEvent(event.getEventID());
		eventSignUp1.setIdVisitor(visitor.getVisitorId());
		
	
		affectedRows = eventSignUpDAO.registerVisitorToEvent(eventSignUp1);
		
		assertTrue(affectedRows);
		
		try {
			
			FERSDataConnection.closeConnection(conn);
		}	catch (DAOException ex) {}
	}

	
	/**
	 * Test for the CheckEventsofVisitor method
	 */
	  @Test
	  public void testCheckEventsofVisitor () {
		
		boolean check;
		
		people = new People();
		people = this.createPeople();
		visitorDAO.insertData(people);
		
		visitor = new Visitor();
		visitor = this.createVisitor();
		visitorDAO.insertData(visitor);
		
		place = new Place();
		place = this.createPlace();
		
		placeDAO.insertPlace(place, "Stadium");
		
		event = new Event();
		event = this.createEvent();
		event.setIdPlace(place.getId());
		
		eventDAO.insertEvent(event);
		
        eventSignUp1 = new EventSignUp();
		
		eventSignUp1.setIdEvent(event.getEventID());
		eventSignUp1.setIdVisitor(visitor.getVisitorId());
		
		eventSignUpDAO.registerVisitorToEvent(eventSignUp1);
		
		check = eventSignUpDAO.checkEventsofVisitor(eventSignUp1);
		
		assertTrue(check);
		
		try {
		
			FERSDataConnection.closeConnection(conn);
		}	catch (DAOException ex) {}
	}
	
	  
	/**
	* Test for the CheckRegisteredEvents method
	*/
	@Test
	public void testCheckRegisteredEvents () {
		
		people = new People();
		people = this.createPeople();
		visitorDAO.insertData(people);
		
		visitor = new Visitor();
		visitor = this.createVisitor();
		visitorDAO.insertData(visitor);
		
		place = new Place();
		place = this.createPlace();
		
		placeDAO.insertPlace(place, "Stadium");
		
		event = new Event();
		event = this.createEvent();
		event.setIdPlace(place.getId());
		
		eventDAO.insertEvent(event);
		
        eventSignUp1 = new EventSignUp();
		
		eventSignUp1.setIdEvent(event.getEventID());
		eventSignUp1.setIdVisitor(visitor.getVisitorId());
		
		eventSignUpDAO.registerVisitorToEvent(eventSignUp1);
		
		
		List<Event> events = new ArrayList<Event>();

		eventSignUp1.setIdVisitor(visitor.getVisitorId());
		events = eventSignUpDAO.registeredEvents(eventSignUp1);
		
		assertEquals(events.size(), 1);
		
		try {

			FERSDataConnection.closeConnection(conn);
		}	catch (DAOException ex) {}
	}
	
	/**
	 * Test for the UnregisteredVisitorToEvent method
	 */
	@Test
	public void testUnregisterVisitorToEvent() {
		boolean affectedRows;
		
		people = new People();
		people = this.createPeople();
		visitorDAO.insertData(people);
		
		visitor = new Visitor();
		visitor = this.createVisitor();
		visitorDAO.insertData(visitor);
		
		place = new Place();
		place = this.createPlace();
		placeDAO.insertPlace(place, "Stadium");
		
		event = new Event();
		event = this.createEvent();
		event.setIdPlace(place.getId());
		
		eventDAO.insertEvent(event);
		
        eventSignUp1 = new EventSignUp();
		
		eventSignUp1.setIdEvent(event.getEventID());
		eventSignUp1.setIdVisitor(visitor.getVisitorId());
		
		eventSignUpDAO.registerVisitorToEvent(eventSignUp1);
		
		affectedRows = eventSignUpDAO.unregisterVisitorToEvent(eventSignUp1);
		
		assertTrue(affectedRows);
		
		try {
		
			FERSDataConnection.closeConnection(conn);
		}	catch (DAOException ex) {}
		
	}
	
}
